"""
This file is part of GSBEdit. For copyright and licensing information, please
see gsbedit.py in the GSBEdit base directory.
"""


import wx
from wx.lib.expando import ExpandoTextCtrl
import ui_elements.controls.attribute_control as ac
from ui_elements.panel_copy import attribute_panel_copy as copy
import sys


class AdditionalPanel(wx.Panel):
    def __init__(self, parent, panel_info, next_id, prevent_freeze = False):
        wx.Panel.__init__(self, parent)
        self.parent = parent
        self.prevent_freeze = prevent_freeze
        
        self.column_sizer = wx.BoxSizer(wx.HORIZONTAL)        
        
        self.columns = 0
        
        self.updaters = []
        
        self.SetSizer(self.column_sizer)
        self.next_id = self.set_panel_info(panel_info, next_id)
    
    
    def get_next_id(self):
        return self.next_id
    
    
    def set_panel_info(self, panel_info, next_id):
        if not(self.prevent_freeze): self.Freeze()
        self.panel_info = copy(panel_info)
                        
        self.columns = len(self.panel_info['items'])
        for column in self.panel_info['items']:
            for control in column:
                control['id'] = next_id
                control['object'] = self.create_control(control)
                control['object'].Hide()
                if 'update_func' in control:
                    self.updaters.append((control, control['update_func'][0], control['update_func'][1]))   # control, func, keys
                next_id += 1
        
        if not(self.prevent_freeze): self.Thaw()
        
        return next_id
    
    
    def set_module(self, module_info, module_values):
        self.update_display()
    
    
    def update_display(self):
        """Update the displayed panels based on the supplied module
        information.
        """
        if not(self.prevent_freeze): self.Freeze()
        self.column_sizer.Clear()
        
        columns = []
        
        for column in self.panel_info['items']:
            current_column = wx.BoxSizer(wx.VERTICAL)
            self.column_sizer.Add(current_column, 1, wx.EXPAND)
            columns.append(current_column)
            for control in column:
                current_column.Add(control['object'], 0, wx.EXPAND|wx.ALL, 5)
                control['object'].Show()
        
        if not(self.prevent_freeze): self.Thaw()
        self.Layout()
        
    
    def should_display(self):
        return None    # Only if something else should be
    
        
    def evt_value_updated(self, key, value, sender=None):
        for control,func,keys in self.updaters:
            if key in keys:
                new_value = func(self.parent.get_current_values())
                if new_value != None:
                    control['object'].SetLabel(new_value)
    
    
    def create_control(self, control):
        if control['type'] == 'text':
            return ExpandoTextCtrl(self, -1, control['text'], style=wx.NO_BORDER)
        else:
            print >> sys.stderr, "Unknown control type specified: %s" % control['type']
            exit(1)
    
    def reset_displayed_values(self):
        pass
